﻿Imports System
Imports System.IO
Imports System.Data.OleDb
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Imaging

Namespace Arwani
    Partial Class Backend_ManageBrandDivCat
        Inherits System.Web.UI.Page

        Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
            Dim arwCon As New ArwaniController
            If Me.cboDivision.SelectedIndex < 0 Then Me.lblError.Text = "Plese Select Division" : Exit Sub
            If Me.cboCategory.SelectedIndex < 0 Then Me.lblError.Text = "Plese Select Category" : Exit Sub
            If Me.txtSortOrder.Text = "" Then Me.lblError.Text = "Please Enter Sort Order" : Exit Sub
            Dim strQ As String = ""
            If CInt(Request.QueryString("id")) <> 0 Then
                strQ = "update brand_division_category set divisionid=" & Me.cboDivision.SelectedItem.Value & ",categoryid=" & Me.cboCategory.SelectedItem.Value & ",brandid=" & CInt(Request.QueryString("bid")) & ",sortorder=" & CInt(Me.txtSortOrder.Text) & " where id=" & CInt(Request.QueryString("id"))
            Else
                strQ = "insert into brand_division_category (divisionid,categoryid,brandid,sortorder) values (" & Me.cboDivision.SelectedItem.Value & "," & Me.cboCategory.SelectedItem.Value & "," & CInt(Request.QueryString("bid")) & "," & CInt(Me.txtSortOrder.Text) & ")"
            End If

            arwCon.ExecuteNonQuery(strQ)
            Response.Redirect("ManageBrandDivCat.aspx?bid=" & CInt(Request.QueryString("bid")))
        End Sub

        Protected Sub cmdBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdBack.Click
            Response.Redirect("ManageBrands.aspx")
        End Sub
        Public Sub loadCategory(ByVal divID As Integer)
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from category where divisionid=" & divID
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            Me.cboCategory.DataTextField = "catname"
            Me.cboCategory.DataValueField = "catid"
            Me.cboCategory.DataSource = dtTable
            Me.cboCategory.DataBind()

        End Sub
        Public Sub loadDivision()
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from division "
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)

            Me.cboDivision.DataSource = dtTable
            Me.cboDivision.DataTextField = "divisiontitle"
            Me.cboDivision.DataValueField = "divisionid"

            Me.cboDivision.DataBind()
            Me.cboDivision.Items.Insert(0, New ListItem("Select Division", 0))
        End Sub
        Public Sub loadData(ByVal brandid As Integer)
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select bdc.id, c.catname, d.divisiontitle,bdc.sortorder from category c, division d,brand_division_category bdc  where d.divisionid=bdc.divisionid and bdc.categoryid=c.catid and  bdc.brandid=" & brandid
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            Me.dgBrandDivCats.DataSource = dtTable
            Me.dgBrandDivCats.DataBind()

        End Sub
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If (Session("ADMIN_USER") IsNot Nothing) Then
                If Page.IsPostBack = True Then Exit Sub
                '  rowCat.Visible = False
                Call loadDivision()
                Call loadData(CInt(Request.QueryString("bid")))
                Call loadEditData(CInt(Request.QueryString("id")))
            Else

                Response.Redirect("Default.aspx")
            End If

        End Sub
        Public Sub loadEditData(ByVal Id As Integer)
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from  brand_division_category  where  id=" & Id
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            If dtTable.Rows.Count > 0 Then
                If Not IsDBNull(dtTable.Rows(0)("divisionid")) Then
                    cboDivision.Items.FindByValue(CInt(dtTable.Rows(0)("divisionid").ToString())).Selected = True
                    Call loadCategory(CInt(dtTable.Rows(0)("divisionid").ToString()))
                    If Not IsDBNull(dtTable.Rows(0)("categoryid")) Then Me.cboCategory.Items.FindByValue(CInt(dtTable.Rows(0)("categoryid").ToString())).Selected = True
                End If
                If Not IsDBNull(dtTable.Rows(0)("sortorder")) Then Me.txtSortOrder.Text = dtTable.Rows(0)("sortorder")

            End If

        End Sub
        Protected Sub dgBrandDivCats_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgBrandDivCats.ItemCommand
            If e.CommandName = "Delete" Then
                Dim arwCon As New ArwaniController
                Dim strQ As String = "delete from brand_division_category where  id=" & CInt(e.CommandArgument)
                arwCon.ExecuteNonQuery(strQ)
                Call loadData(CInt(Request.QueryString("bid")))
            ElseIf e.CommandName = "Edit" Then
                Response.Redirect("ManageBrandDivCat.aspx?bid=" & CInt(Request.QueryString("bid")) & "&id=" & CInt(e.CommandArgument))
            End If
        End Sub 

        Protected Sub cboDivision_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboDivision.SelectedIndexChanged
            If Me.cboDivision.SelectedIndex = -1 Then Exit Sub
            Call loadCategory(Me.cboDivision.SelectedItem.Value)
        End Sub

        Protected Sub dgBrandDivCats_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgBrandDivCats.ItemDataBound
            Try

                Dim ID As Integer = dgBrandDivCats.DataKeys(e.Item.ItemIndex)
                Dim txtSortOrder As TextBox = CType(e.Item.FindControl("txtSortOrder"), TextBox)
                txtSortOrder.Attributes.Add("onblur", "postSortOrder(this.value,'brand_division_category', 'id','" & ID & "')")
            Catch ex As Exception

            End Try
        End Sub
    End Class
End Namespace